package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Prod;
import com.powernode.model.Result;
import com.powernode.service.ProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/2/24 12:14
 */
@Slf4j
@RequestMapping("prod/prod")
@RestController
@Api(tags = "商品接口管理")
public class ProdController {

    @Autowired
    private ProdService prodService;

    /**
     * 分页查询商品
     * @param current
     * @param size
     * @param prodName
     * @param status
     * @return
     */
    @ApiOperation("分页查询商品")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page> loadProduct(@RequestParam(value = "current") Long current,
                                 @RequestParam(value = "size") Long size,
                                 @RequestParam(value = "prodName", required = false) String prodName,
                                 @RequestParam(value = "status",required = false) Long status
                                 ){
        Page page = new Page<Prod>(current, size);

        page = prodService.page(page, new LambdaQueryWrapper<Prod>()
                .like(StringUtils.hasText(prodName), Prod::getProdName, prodName)
                .eq(status != null, Prod::getStatus, status)
                .orderByDesc(Prod::getCreateTime));


        return Result.success(page);
    }

    /**
     * 查看商品
     * @param prodId
     * @return
     */
    @ApiOperation("查看商品")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> getProdById(@PathVariable Long prodId){
        Prod byId = prodService.getProdById(prodId);


        return Result.success(byId);
    }

    /**
     * 新增商品
     * @return
     */
    @ApiOperation("新增商品")
    @PostMapping()
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod){

        log.info("新增商品:{}",prod);
        boolean b = prodService.saveProd(prod);

        return Result.handle(b);
    }

    /**
     * 修改商品
     * @return
     */
    @ApiOperation("修改商品")
    @PutMapping()
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> updateProd(@RequestBody Prod prod){
        boolean b = prodService.modifyProd(prod);
        return Result.handle(b);
    }

    /**
     * 删除商品&批量删除商品
     * @return
     */
    @ApiOperation("xxx")
    @DeleteMapping("{prodIds}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> deleteProdByIds(@PathVariable("prodIds")List<Long> prodIds){
        boolean b = prodService.removeProdByIds(prodIds);

        return Result.handle(b);
    }

    ///////////////////////////////////////feign接口//////////////////////////////////////////

    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByProdIds(@RequestParam List<Long> ids){
        List<Prod> prods = prodService.listByIds(ids);
        return Result.success(prods);
    }


}
